﻿#include "CHKAIDevMange.h"
#include "CSqlOper.h"
#include "Public.h"
#include "CGlobal.h"
#include "CVideoDevCfgOper.h"
#include "Header.h"
#include "CVideoPlayer.h"
#include "CVideoPlayDouble.h"
#include "CLogWidget.h"
#include "CVideoState.h"
#include "CAlarmTable.h"
#include "CStatisticsChat.h"
#include "CVideoLargeDlg.h"
#include "CPubAlarmTable.h"
#include "CPucStatisicsChat.h"
#include "CAlarmPoint.h"

CHKAIDevMange::CHKAIDevMange(QString sDevGuid, QString sDevName, bool& bRet)
{
	//首先根据DevGuid关联出声振温设备的设备GUID
	QVector<QSqlRecord> vecRecord;
	QString sErrMsg;
	bool bSqlRet=CSqlOper::instance().selectDevInfo(sDevGuid, vecRecord, sErrMsg);
	if (!bSqlRet)
	{
		messageBox(1, A2T("数据库连接失败"));
		bRet = false;
		return;
	}
	QString sCamerDevGuid = "";
	if (vecRecord.size() > 0)
	{
		for (int i = 0; i < vecRecord.size(); i++)
		{
			sCamerDevGuid = vecRecord[i].value("CAMER_DEV_GUID").toString();
		}
		m_sDevGuid = sCamerDevGuid;
		m_sDevName = sDevName;
		m_pDev = new CDevCamer(sCamerDevGuid, sDevName, bRet);
	}
	else if (vecRecord.size() == 0)
	{
		sCamerDevGuid = createGuid();
		bSqlRet=CSqlOper::instance().insertDevInfo(sDevGuid, sDevName, sCamerDevGuid, sErrMsg);
		if (!bSqlRet)
		{
			bRet = false;
			return;
		}
		m_sDevGuid = sCamerDevGuid;
		m_sDevName = sDevName;
		m_pDev = new CDevCamer(sCamerDevGuid, sDevName, bRet);
	}
}

CHKAIDevMange::~CHKAIDevMange()
{
	if (m_pDev != nullptr)
	{
		delete m_pDev;
		m_pDev = nullptr;
	}
}

bool CHKAIDevMange::startDev()
{
	if (m_pDev->m_pCamerCtrMange != nullptr)
	{
		m_pDev->m_pCamerCtrMange->ThreadStart();
	}
	if (m_pDev->m_pVideoWorkMange != nullptr)
	{
		m_pDev->m_pVideoWorkMange->threadStart();
	}
	return true;
}

void CHKAIDevMange::stopDev()
{
	if (m_pDev->m_pCamerCtrMange != nullptr)
	{
		m_pDev->m_pCamerCtrMange->ThreadStop();
	}
	if (m_pDev->m_pVideoWorkMange != nullptr)
	{
		m_pDev->m_pVideoWorkMange->threadStop();
	}
}

QWidget* CHKAIDevMange::getConfigWidget()
{
	if (m_pDev == nullptr || m_pDev->m_pDevData == nullptr)
		return nullptr;
	QWidget* pWidget = new CVideoDevCfgOper(m_pDev->m_pDevData);
	return pWidget;
}

QWidget* CHKAIDevMange::createWidget(EWidgetType enWidgetType)
{
	if (m_pDev == nullptr)
	{
		return nullptr;
	}
	QWidget* pWidget = nullptr;
	switch (enWidgetType)
	{
	case enCamerPlayer:
		pWidget = new CVideoPlayer(m_pDev);
		pWidget->setProperty("row", 1);
		pWidget->setProperty("col", 1);
		pWidget->setProperty("name", A2T("摄像头监控画面"));
		break;
	case enCamerPlayerDouble:
		pWidget = new CVideoPlayDouble(m_pDev);
		pWidget->setProperty("row", 1);
		pWidget->setProperty("col", 2);
		pWidget->setProperty("name", A2T("双目摄像头监控画面"));
		break;
	case enLog:
		pWidget = new CLogWidget(m_pDev->m_pDevData->m_sCamerName);
		pWidget->setProperty("row", 1);
		pWidget->setProperty("col", 2);
		pWidget->setProperty("name", A2T("日志模块"));
		break;
	case enUVideoState:
		pWidget = new CVideoState(m_pDev->m_pDevData);
		pWidget->setProperty("row", 1);
		pWidget->setProperty("col", 1);
		pWidget->setProperty("name", A2T("摄像头状态卡片"));
		break;
	case enAlarmTable:
		pWidget = new CAlarmTable(m_pDev->m_pDevData);
		pWidget->setProperty("row", 1);
		pWidget->setProperty("col", 2);
		pWidget->setProperty("name", A2T("摄像头报警事件列表卡片"));
		break;
	case enStatisticsChart:
		pWidget = new CStatisticsChat(m_pDev->m_pDevData);
		pWidget->setProperty("row", 2);
		pWidget->setProperty("col", 1);
		pWidget->setProperty("name", A2T("摄像头报警统计卡片"));
		break;
	case enPubAlarmTableX:
		pWidget = new CPubAlarmTable(1, "123");//只获取设备列表，并不需要绑定设备的操作
		pWidget->setProperty("row", 1);
		pWidget->setProperty("col", 2);
		pWidget->setProperty("name", A2T("报警事件表格公共卡片"));
		break;
	case enPubStatisticsChartX:
		pWidget = new CPucStatisicsChat(1, "456");//只获取设备列表，并不需要绑定设备的操作
		pWidget->setProperty("row", 2);
		pWidget->setProperty("col", 2);
		pWidget->setProperty("name", A2T("报警事件统计图公共卡片"));
		break;
	default:
		break;
	}
	return pWidget;
}

QWidget* CHKAIDevMange::getDeviceRunUi()
{
	if (m_pDev == nullptr)
		return nullptr;
	CVideoLargeDlg* pDlg = new CVideoLargeDlg(m_pDev,0);
	return pDlg;
}

bool CHKAIDevMange::ctrDev(SCtrInfo stCtrInfo)
{
	if (m_pDev == nullptr)
	{
		return false;
	}
	return m_pDev->ctrDev(stCtrInfo);
}

QList<CMeasurePoint*> CHKAIDevMange::getMeasurePoints()
{
	QList<CMeasurePoint*> list;
	for (QMap<EDevPointType, CMeasurePoint*>::iterator itor = m_pDev->m_pDevData->m_mapDevPoint.begin(); itor != m_pDev->m_pDevData->m_mapDevPoint.end(); itor++)
	{
		list.append(itor.value());
	}
	return list;
}

QString CHKAIDevMange::getDeviceStatus()
{
	if (m_pDev == nullptr)
		return "";
	QString sRet = "";
	if (m_pDev->m_pDevData->m_bLogin == true)
	{
		sRet += A2T("摄像头:在线 ");
	}
	else if (m_pDev->m_pDevData->m_bLogin == false)
	{
		sRet += A2T("摄像头:离线 ");
	}
	return sRet;
}

QVector<QWidget*> CHKAIDevMange::getDeviceUiList()
{
	QVector<QWidget*> vecWidget;
	if (m_pDev == nullptr)
	{
		return vecWidget;
	}

	{
		QWidget* pWidget = nullptr;
		pWidget = new CVideoPlayer(m_pDev);
		pWidget->setProperty("row", 1);
		pWidget->setProperty("col", 1);
		pWidget->setProperty("name", A2T("摄像头监控画面"));
		pWidget->hide();
		vecWidget.push_back(pWidget);
	}

	{
		QWidget* pWidget = nullptr;
		pWidget = new CVideoPlayDouble(m_pDev);
		pWidget->setProperty("row", 1);
		pWidget->setProperty("col", 2);
		pWidget->setProperty("name", A2T("双目摄像头监控画面"));
		pWidget->hide();
		vecWidget.push_back(pWidget);
	}

	{
		QWidget* pWidget = nullptr;
		pWidget = new CLogWidget(m_pDev->m_pDevData->m_sCamerName);
		pWidget->setProperty("row", 1);
		pWidget->setProperty("col", 2);
		pWidget->setProperty("name", A2T("日志模块"));
		pWidget->hide();
		vecWidget.push_back(pWidget);
	}

	{
		QWidget* pWidget = nullptr;
		pWidget = new CVideoState(m_pDev->m_pDevData);
		pWidget->setProperty("row", 1);
		pWidget->setProperty("col", 1);
		pWidget->setProperty("name", A2T("摄像头状态卡片"));
		pWidget->hide();
		vecWidget.push_back(pWidget);
	}

	{
		QWidget* pWidget = nullptr;
		pWidget = new CAlarmTable(m_pDev->m_pDevData);
		pWidget->setProperty("row", 1);
		pWidget->setProperty("col", 2);
		pWidget->setProperty("name", A2T("摄像头报警事件列表"));
		pWidget->hide();
		vecWidget.push_back(pWidget);
	}

	{
		QWidget* pWidget = nullptr;
		pWidget = new CStatisticsChat(m_pDev->m_pDevData);
		pWidget->setProperty("row", 2);
		pWidget->setProperty("col", 1);
		pWidget->setProperty("name", A2T("摄像头报警统计卡片"));
		pWidget->hide();
		vecWidget.push_back(pWidget);
	}

	{
		QWidget* pWidget = nullptr;
		pWidget = new CPubAlarmTable(0,"");
		pWidget->setProperty("row", 1);
		pWidget->setProperty("col", 2);
		pWidget->setProperty("name", A2T("报警事件表格公共卡片"));
		pWidget->hide();
		vecWidget.push_back(pWidget);
	}

	{
		QWidget* pWidget = nullptr;
		pWidget = new CPucStatisicsChat(0, "");
		pWidget->setProperty("row", 2);
		pWidget->setProperty("col", 2);
		pWidget->setProperty("name", A2T("报警事件统计图公共卡片"));
		pWidget->hide();
		vecWidget.push_back(pWidget);
	}
	return vecWidget;
}

QMap<QString, QString> CHKAIDevMange::getInputPointList()
{
	return m_pDev->m_pDevData->m_mapInputPointAddr;
}

void CHKAIDevMange::setInputPointList(QMap<QString, CMeasurePoint*>vecInputPoint)
{
	if (m_pDev == nullptr || m_pDev->m_pDevData == nullptr)
		return;
	if (vecInputPoint.size() == 0)
		return;
	for (auto itor = vecInputPoint.begin(); itor != vecInputPoint.end(); itor++)
	{
		QString sTagId = itor.key();
		CMeasurePoint* pPoint = itor.value();
		if (pPoint == nullptr)
		{
			continue;
		}
		QString sTagAddr = pPoint->getCompletepath();

		if (m_pDev->m_pDevData->m_mapInputPoint.contains(sTagId) && m_pDev->m_pDevData->m_mapInputPointAddr.contains(sTagId))
		{
			m_pDev->m_pDevData->m_mapInputPoint[sTagId] = pPoint;
			//查询数据库是否存在这个对应关系
			QString sErrMsg = "";
			QVector<QSqlRecord> vecRecord;
			CSqlOper::instance().selectTagID(m_pDev->m_pDevData->m_sDevGuid, sTagId, vecRecord, sErrMsg);
			if (vecRecord.size() == 0)
			{
				//在数据库中添加测点地址配置
				CSqlOper::instance().insertTagID(m_pDev->m_pDevData->m_sDevGuid, sTagId, sTagAddr, sErrMsg);
			}
			else
			{
				//更新测点地址配置
				CSqlOper::instance().updateTagID(m_pDev->m_pDevData->m_sDevGuid, sTagId, sTagAddr, sErrMsg);
			}
		}
	}
}

void CHKAIDevMange::deleteDev(QString sDevID)
{
	//删除设备信息绑定表
	QString sErrMsg = "";
	CSqlOper::instance().deleteDevInfo(sDevID, sErrMsg);
	//删除摄像头配置表相关数据
	CSqlOper::instance().deleteCamerInfo(m_sDevGuid, sErrMsg);
}
